"""
预设的大模型服务商配置

包含主流支持 OpenAI Compatible API 的服务商配置
"""

from typing import Dict, Any, Optional

# 预设配置字典
PRESET_CONFIGS = {
    "zhipu": {
        "name": "智谱清言",
        "base_url": "https://open.bigmodel.cn/api/paas/v4/",
        "model_name": "glm-4",
        "supports_function_calling": True,
        "max_tokens": 4096,
        "description": "智谱AI的GLM-4模型，支持中文对话和函数调用"
    },
    "zhipu-turbo": {
        "name": "智谱清言 Turbo",
        "base_url": "https://open.bigmodel.cn/api/paas/v4/",
        "model_name": "glm-4-0520",
        "supports_function_calling": True,
        "max_tokens": 4096,
        "description": "智谱AI的GLM-4-0520模型，更快的响应速度"
    },
    "moonshot": {
        "name": "月之暗面 Kimi",
        "base_url": "https://api.moonshot.cn/v1/",
        "model_name": "moonshot-v1-8k",
        "supports_function_calling": True,
        "max_tokens": 8192,
        "description": "月之暗面的Kimi模型，支持长文本处理"
    },
    "moonshot-32k": {
        "name": "月之暗面 Kimi 32K",
        "base_url": "https://api.moonshot.cn/v1/",
        "model_name": "moonshot-v1-32k",
        "supports_function_calling": True,
        "max_tokens": 32768,
        "description": "月之暗面的Kimi 32K模型，支持超长文本处理"
    },
    "deepseek": {
        "name": "DeepSeek",
        "base_url": "https://api.deepseek.com/v1/",
        "model_name": "deepseek-chat",
        "supports_function_calling": True,
        "max_tokens": 4096,
        "description": "DeepSeek的对话模型"
    },
    "qwen": {
        "name": "通义千问",
        "base_url": "https://dashscope.aliyuncs.com/compatible-mode/v1/",
        "model_name": "qwen-turbo",
        "supports_function_calling": True,
        "max_tokens": 6000,
        "description": "阿里云通义千问模型"
    },
    "qwen-max": {
        "name": "通义千问 Max",
        "base_url": "https://dashscope.aliyuncs.com/compatible-mode/v1/",
        "model_name": "qwen-max",
        "supports_function_calling": True,
        "max_tokens": 6000,
        "description": "阿里云通义千问Max模型，性能更强"
    },
    "openai": {
        "name": "OpenAI GPT",
        "base_url": "https://api.openai.com/v1/",
        "model_name": "gpt-4",
        "supports_function_calling": True,
        "max_tokens": 4096,
        "description": "OpenAI的GPT-4模型"
    },
    "openai-turbo": {
        "name": "OpenAI GPT-3.5 Turbo",
        "base_url": "https://api.openai.com/v1/",
        "model_name": "gpt-3.5-turbo",
        "supports_function_calling": True,
        "max_tokens": 4096,
        "description": "OpenAI的GPT-3.5 Turbo模型，性价比高"
    },
    "claude": {
        "name": "Claude (via OpenAI Compatible)",
        "base_url": "https://api.anthropic.com/v1/",
        "model_name": "claude-3-sonnet-20240229",
        "supports_function_calling": False,
        "max_tokens": 4096,
        "description": "Anthropic的Claude模型（需要兼容层）"
    }
}


def get_preset_config(provider_name: str) -> Optional[Dict[str, Any]]:
    """获取预设配置
    
    Args:
        provider_name: 提供商名称
        
    Returns:
        配置字典，如果不存在则返回 None
    """
    return PRESET_CONFIGS.get(provider_name)


def list_available_providers() -> Dict[str, str]:
    """列出所有可用的提供商
    
    Returns:
        提供商名称到描述的映射
    """
    return {
        name: config["description"] 
        for name, config in PRESET_CONFIGS.items()
    }


def get_providers_by_feature(feature: str) -> Dict[str, Dict[str, Any]]:
    """根据功能特性筛选提供商
    
    Args:
        feature: 功能特性名称（如 "supports_function_calling"）
        
    Returns:
        支持该功能的提供商配置
    """
    return {
        name: config 
        for name, config in PRESET_CONFIGS.items()
        if config.get(feature, False)
    }


def validate_preset_config(config: Dict[str, Any]) -> bool:
    """验证预设配置的完整性
    
    Args:
        config: 配置字典
        
    Returns:
        是否有效
    """
    required_fields = ["name", "base_url", "model_name"]
    return all(field in config for field in required_fields)


# 环境变量映射
ENV_VAR_MAPPING = {
    "zhipu": "ZHIPU_API_KEY",
    "zhipu-turbo": "ZHIPU_API_KEY", 
    "moonshot": "MOONSHOT_API_KEY",
    "moonshot-32k": "MOONSHOT_API_KEY",
    "deepseek": "DEEPSEEK_API_KEY",
    "qwen": "QWEN_API_KEY",
    "qwen-max": "QWEN_API_KEY",
    "openai": "OPENAI_API_KEY",
    "openai-turbo": "OPENAI_API_KEY",
    "claude": "CLAUDE_API_KEY"
}


def get_env_var_name(provider_name: str) -> Optional[str]:
    """获取提供商对应的环境变量名
    
    Args:
        provider_name: 提供商名称
        
    Returns:
        环境变量名，如果不存在则返回 None
    """
    return ENV_VAR_MAPPING.get(provider_name)
